import { Injectable } from '@nestjs/common';
import { TodosModel } from '@/model';
import status from '@/config/status';
import contant from '@/config/constant';
import { Logger } from 'nest-logs';

@Injectable()
export class TodoListService {
  // 创建待办
  async createTodo(parmas: any): Promise<any> {
    const todo = await TodosModel.create({
      todoContent: parmas?.todoContent,
    });
    return {
      todoContent: todo?.todoContent,
      ...status.SUCCESS,
    };
  }

  // 完成待办
  async finishTodo(params: any): Promise<any> {
    // isFinish 0 完成 1 未完成
    const todo = await TodosModel.update(
      {
        isFinish: !params?.isFinish,
      },
      {
        where: {
          id: params?.id,
        },
      },
    );
    const message = !todo[0] ? status.FINISH_FAIL : status.SUCCESS;
    return {
      id: params?.id,
      ...message,
    };
  }

  // 删除待办
  async deleteTodo(params: any): Promise<any> {
    // isFinish 0 完成 1 未完成
    const todo = await TodosModel.destroy({
      where: {
        id: params?.id,
      },
    });
    const message = !todo ? status.DELETE_FAIL : status.SUCCESS;
    return {
      id: params?.id,
      ...message,
    };
  }

  // 编辑待办
  async editTodo(params: any): Promise<any> {
    // isFinish 0 完成 1 未完成
    const todo = await TodosModel.update(
      {
        todoContent: params?.todoContent,
      },
      {
        where: {
          id: params?.id,
        },
      },
    );
    const message = !todo[0] ? status.EDIT_FAIL : status.SUCCESS;
    return {
      id: params?.id,
      ...message,
    };
  }

  // 获取待办列表
  async getTodoList(parmas: any): Promise<any> {
    const { SELECT_TODO_TYPE } = contant;
    const { type = 0, pageIndex = 1, pageCount = 10 } = parmas;
    switch (type) {
      // 全部待办
      case SELECT_TODO_TYPE[0]?.value:
        {
          const { count, rows: todos } = await TodosModel.findAndCountAll({
            limit: pageCount,
            offset: pageCount * (pageIndex - 1),
            order: [['updatedAt', 'DESC']],
          });
          return {
            data: todos?.sort((a, b) => a?.isFinish < b?.isFinish),
            page: {
              pageIndex: pageIndex,
              pageCount: pageCount,
              totalCount: count,
            },
            ...status.SUCCESS,
          };
        }
        break;
      // 未完成的待办
      case SELECT_TODO_TYPE[1]?.value:
        {
          {
            const { count, rows: todos } = await TodosModel.findAndCountAll({
              where: {
                isFinish: true,
              },
              limit: pageCount,
              offset: pageCount * (pageIndex - 1),
              order: [['updatedAt', 'DESC']],
            });
            return {
              data: todos,
              page: {
                pageIndex: pageIndex,
                pageCount: pageCount,
                totalCount: count,
              },
              ...status.SUCCESS,
            };
          }
        }
        break;
      // 已完成的待办
      case SELECT_TODO_TYPE[2]?.value:
        {
          {
            const { count, rows: todos } = await TodosModel.findAndCountAll({
              where: {
                isFinish: false,
              },
              limit: pageCount,
              offset: pageCount * (pageIndex - 1),
              order: [['updatedAt', 'DESC']],
            });
            return {
              data: todos,
              page: {
                pageIndex: pageIndex,
                pageCount: pageCount,
                totalCount: count,
              },
              ...status.SUCCESS,
            };
          }
        }
        break;
    }
  }
}
